package cn.changeforyou.web.cloud.devUtilApi.db.sql.fineReport;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 1.0
 * @description 帆软sql脚本工具
 * @auther zhyu
 * 2022-9-14 18:07
 */
@Slf4j
public class FineReportSqlScriptUtil {
    static Pattern scriptPattern = Pattern.compile("\\$\\{(.*?)\\}");
    static Pattern ifPattern = Pattern.compile("[if]\\S?\\((.*)\\)");
    static Pattern lengthPattern = Pattern.compile("len\\S?\\((.*?)\\).*?,");
    static Pattern ifParam2And3SingleQuotes = Pattern.compile("'(.*)'\\s*,\\s*'.*'");
    static Pattern ifParam2And3DoubleQuotes = Pattern.compile("\"(.*)\"\\s*,\\s*\".*\"");
    static Pattern replaceVarNameDoubleQuotes = Pattern.compile(".*(\").*(\\+.*\\+).*(\")");
    static Pattern replaceVarNameSingleQuotes = Pattern.compile(".*(').*(\\+.*\\+).*(')");

    /**
     * 帆软sql脚本转ongl
     */
    public static String fineReportSqlScript2SqlWithOngl(String fineReportSqlScript) {
        Matcher matcher = scriptPattern.matcher(fineReportSqlScript);
        StringBuilder sb = new StringBuilder();
        int start = 0;
        int end = -1;
        while (matcher.find()) {
            String group1 = matcher.group(1);
            int mStart = matcher.start(0);
            int mEnd = matcher.end(0);
            end = mStart;
            sb.append(fineReportSqlScript.substring(start, end));
            String replaceStr = dealSubString(group1);
            sb.append(replaceStr);
            start = mEnd;
        }
        return sb.toString();
    }

    /**
     * 处理子句
     */
    private static String dealSubString(String subString) {
        if (log.isDebugEnabled()) {
            log.debug("待处理的字句:\n {}", subString);
        }
        subString = subString.trim();
        if (subString.startsWith("if")) {
            return dealIf(subString);
        } else if (subString.startsWith("IF")) {
            return dealIf(subString);
        } else {
            return "自居";
        }
    }

    /**
     * 处理if
     */
    private static String dealIf(String subString) {
        Matcher matcher = ifPattern.matcher(subString);
        if (matcher.find()) {
            String group = matcher.group(1);
            if (log.isDebugEnabled()) {
                log.debug("找到帆软if函数的参数\n: {}", group);
            }
            Matcher lengthMatcher = lengthPattern.matcher(group);
            if (lengthMatcher.find()) {
                String varName = lengthMatcher.group(1);
                if (log.isDebugEnabled()) {
                    log.debug("变量名: {}", varName);
                }
                int index = lengthMatcher.end(0);
                String params = group.substring(index);
                if (log.isDebugEnabled()) {
                    log.debug("要处理的if函数后两个参数是: {}", params);
                }
                Matcher paramsMatcher = null;
                boolean doubleQuotes = params.startsWith("\"");
                if (doubleQuotes) {
                    paramsMatcher = ifParam2And3DoubleQuotes.matcher(params);
                } else {
                    paramsMatcher = ifParam2And3SingleQuotes.matcher(params);
                }
                if (paramsMatcher.find()) {
                    String param2 = paramsMatcher.group(1);
                    if (log.isDebugEnabled()) {
                        log.debug("要处理的if函数的第二个参数是: {}", param2);
                    }
                    StringBuilder sb = new StringBuilder();
                    Matcher replaceMatcher = null;
                    if (doubleQuotes) {
                        replaceMatcher = replaceVarNameDoubleQuotes.matcher(param2);
                    } else {
                        replaceMatcher = replaceVarNameSingleQuotes.matcher(param2);
                    }
                    if (replaceMatcher.find()) {
                        String replace = replaceMatcher.group(2);
                        replace = replace.replace("+", "").trim();
                        if (log.isDebugEnabled()) {
                            log.debug("需要被替换的参数2是: {}", replace);
                        }
                        int start = replaceMatcher.start(1);
                        int end = replaceMatcher.end(3);
                        sb.append(param2.substring(0, start));
                        sb.append("${" + replace + "}");
                        sb.append(param2.substring(end));
                    }
                    return sb.toString();
                }
            }
        }
        return "IF";
    }
}
